package com.lzc.springboot_jsp_shiro.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 获取spring工厂bean的工具类
 */
@Component
public class ApplicationContextUtils implements ApplicationContextAware {

    private static ApplicationContext context;

    /**
     * 该方法获取spring容器
     *
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //设置容器
        context = applicationContext;
    }

    //根据bean的名字获取工厂中指定的bean对象
    public static Object getBean(String name) {
        return context.getBean(name);
    }
}
